package com.yenroc.invoice.constant;

import java.util.Optional;

public interface BaseEnum<T> {

    T getCode();

    String getDesc();

    static <T, E extends BaseEnum<T>> E getByCode(T code, Class<E> enumClass) {
        for (E e : enumClass.getEnumConstants())
            if (e.getCode().equals(code)) return e;
        return null;
    }

    static <T> String getDescOrBlank(BaseEnum<T> baseEnum) {
        return Optional.ofNullable(baseEnum)
                .map(BaseEnum::getDesc)
                .orElse("");
    }

    static <T, E extends BaseEnum<T>> String getDescOrBlank(T code, Class<E> enumClass) {
        return Optional.ofNullable(getByCode(code, enumClass))
                .map(BaseEnum::getDescOrBlank)
                .orElse("");
    }

}